﻿using System;
using System.Collections.Generic;
using System.IO;
using System.Reflection;
using System.Windows.Forms;

namespace SksApp
{
    public enum Fonksiyonlar : byte
    {
        KasaRaporu = 0,
        UrunDegisimi = 1,
        MenuKapat = 2,
        ProgramKapat = 3,
        BelgeSecimi = 4,
        IadeBelge = 5,
        FaturaBelge = 6,
        FisBelge = 7,
        MasrafOraniGiris = 8,
        EtiketBaski = 9,
        RafEtiketBaski = 10,
        UrunEtiketBaski = 11,
        KasayaGonder = 12,
        PluGonder = 13,
        Departmangonder = 14,
        MasrafGiris = 15,
        MasrafTutariGiris = 16,
        IskontoGiris = 17,
        IskontoOranGiris = 18,
        IskontoTutarGiris = 19,
        FiyatDegistir = 20,
        FiyatNo1 = 21,
        FiyatNo2 = 22,
        FiyatNo3 = 23,
        FiyatNo4 = 24,
        FiyatNo5 = 25,
        FiyatNo6 = 30,
        FiyatNo7 = 31,
        FiyatNo8 = 32,
        FiyatNo9 = 33,
        FiyatNo10 = 34,
        TeraziyeVeriGonder = 26,
        FiyatGirisi = 27,
        AlisverisKontrolListesi = 28,
        MagazaMasrafGiris = 29,
        GunlukKasaRaporuGoruntule = 35,
        GunlukKasiyerRaporuGoruntule = 36,
        Raporlar = 37,
        CekmeceAc = 38
    }

    public class KeyProcess
    {
        Dictionary<int, KeyType> _keys = new Dictionary<int, KeyType>();
        public BaseView OwnerForm { get; set; }

        public KeyProcess(BaseView owner)
        {
            this.OwnerForm = owner;
            if (File.Exists(Application.StartupPath + "\\keys.ini"))
            {
                using (StreamReader sr = new StreamReader(Application.StartupPath + "\\keys.ini"))
                {
                    string line = "";
                    string[] alanlar;
                    while (sr.Peek() > -1)
                    {
                        line = sr.ReadLine();
                        alanlar = line.Split(';');
                        _keys.Add(int.Parse(alanlar[0]), new KeyType()
                        {
                            Alt = Cevir.Parse<bool>(alanlar[1]),
                            Shift = Cevir.Parse<bool>(alanlar[2]),
                            Ctrl = Cevir.Parse<bool>(alanlar[3]),
                            Name = alanlar[4]
                        });
                    }
                    sr.Close();
                }
            }
        }

        ~KeyProcess()
        {
            using (StreamWriter sw = new StreamWriter(Application.StartupPath + "\\keys.ini", false))
            {
                foreach (KeyValuePair<int, KeyType> key in _keys)
                {
                    sw.WriteLine(string.Format("{0};{1};{2};{3};{4}", key.Key, Convert.ToByte(key.Value.Alt), Convert.ToByte(key.Value.Shift), Convert.ToByte(key.Value.Ctrl), key.Value.Name));
                }
                sw.Close();
            }
        }

        public void AddShortCutKey(Button btn, int key, bool alt, bool ctrl, bool shift)
        {
            if (!_keys.ContainsKey(key))
            {
                _keys.Add(key, new KeyType() { Name = btn.Name, Alt = alt, Ctrl = ctrl, Shift = shift });
            }
            else
            {
                _keys[key].Alt = alt;
                _keys[key].Ctrl = ctrl;
                _keys[key].Shift = shift;
                _keys[key].Name = btn.Name;
            }
        }

        //public void AddShortCutMenuKey(VistaMenuControl.VistaMenuItem btn, int key, bool alt, bool ctrl, bool shift)
        //{
        //    if (!_keys.ContainsKey(key))
        //    {
        //        _keys.Add(key, new KeyType() { Name = btn.ItemTag.ToString(), Alt = alt, Ctrl = ctrl, Shift = shift });
        //    }
        //    else
        //    {
        //        _keys[key].Alt = alt;
        //        _keys[key].Ctrl = ctrl;
        //        _keys[key].Shift = shift;
        //        _keys[key].Name = btn.ItemTag.ToString();
        //    }
        //}

        public void ExecuteEvent(int keydata, Form frm)
        {
            if (_keys.ContainsKey(keydata))
            {
                var sonuc = frm.Controls.Find(_keys[keydata].Name, true);
                if (sonuc != null && sonuc.Length > 0)
                {
                    Button btn = sonuc[0] as Button;
                    Type t = btn.GetType();
                    object[] p = new object[1];
                    p[0] = EventArgs.Empty;
                    MethodInfo m = t.GetMethod("OnClick", BindingFlags.NonPublic | BindingFlags.Instance);
                    m.Invoke(btn, p);
                }
                else
                {
                    Fonksiyonlar fn = (Fonksiyonlar)Enum.Parse(typeof(Fonksiyonlar), _keys[keydata].Name);
                    PosView main = (PosView)this.OwnerForm;
                    main.FonksiyonCalistir(fn);
                }
            }
        }
    }

    public class KeyType
    {
        public string Name { get; set; }
        public bool Alt { get; set; }
        public bool Shift { get; set; }
        public bool Ctrl { get; set; }
    }
}
